package me.zhengjie.pm.projectdepartment.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.pm.projectdepartment.domain.ProjectDepartment;
import me.zhengjie.pm.projectdepartment.service.ProjectDepartmentService;
import me.zhengjie.pm.projectdepartment.service.dto.ProjectDepartmentQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Api(tags = "项目-部门职责管理")
@RestController
@RequestMapping("/api/projectDepartment")
public class ProjectDepartmentController {

    private final ProjectDepartmentService projectDepartmentService;

    public ProjectDepartmentController(ProjectDepartmentService projectDepartmentService) {
        this.projectDepartmentService = projectDepartmentService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectDepartment:list')")
    public void download(HttpServletResponse response, ProjectDepartmentQueryCriteria criteria) throws IOException {
        projectDepartmentService.download(projectDepartmentService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询项目-部门职责")
    @ApiOperation("查询项目-部门职责")
    @PreAuthorize("@el.check('projectDepartment:list')")
    public ResponseEntity<Object> getProjectDepartments(ProjectDepartmentQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectDepartmentService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目-部门职责")
    @ApiOperation("新增项目-部门职责")
    @PreAuthorize("@el.check('projectDepartment:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ProjectDepartment resources){
        return new ResponseEntity<>(projectDepartmentService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改项目-部门职责")
    @ApiOperation("修改项目-部门职责")
    @PreAuthorize("@el.check('projectDepartment:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ProjectDepartment resources){
        projectDepartmentService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除项目-部门职责")
    @ApiOperation("删除项目-部门职责")
    @PreAuthorize("@el.check('projectDepartment:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        projectDepartmentService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}